<?php
namespace App\Http\Controllers\Supplier;
use App\Models\MemberAddress;
use App\Services\CommonService;
use Illuminate\Http\Request;

//归还地址
class ReturnAddressController extends BaseController
{
    //归还地址列表
    public function returnAddressList(Request $request)
    {
        $input = $request->input();
        $page = $input['page'] ? $input['page'] : 1;
        $pageSize = $input['pageSize'] ? $input['pageSize'] : CommonService::PAGESIZE;
        $data = MemberAddress::GetReturnAddressList($pageSize, array('member_id' => $this->shopUser['member_id'], 'is_delete' => MemberAddress::MEMBER_ADDRESS_NOT_DELETE));
        $data = formatPage($data);
        $info = $data['data'];
        foreach ($info as $k => $v) {
            $info[$k]['addtime'] = !empty($v['addtime']) ? dateFormat($v['addtime']) : '';
        }
        $data['data'] = $info;
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '获取成功', $data);
    }

    //根据id查询归还地址
    public function returnAddressById(Request $request)
    {
        $input = $request->input();
        if (empty($input['id'])) {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, 'id为空');
        }
        $data = MemberAddress::GetReturnAddressById($input['id']);
        $data['addtime'] = !empty($data['addtime']) ? dateFormat($data['addtime']) : '';
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '获取成功', $data);
    }

    //添加或编辑归还地址
    public function addOrEditReturnAddress(Request $request)
    {
        $input = $request->input();
        if (empty($input['id'])) { //添加
            if (!Verify_Phone($input['mobile'])) {
                return ReturnMsg(CommonService::HTTP_CODE_FAIL, '请输入有效手机号');
            }
            if ($input['isdefault'] == MemberAddress::MEMBER_ADDRESS_IS_DEFAULT) {  //是否默认  1 是  2否 isdefault=1时 要把member_id相同的情况下其他条数据的isdefault=1变成2
                MemberAddress::where(array('member_id' => $this->shopUser['member_id']))->update(array('isdefault' => MemberAddress::MEMBER_ADDRESS_NOT_DEFAULT));
            }
            $addData = array(
                'realname' => $input['realname'],
                'mobile' => $input['mobile'],
                'province' => $input['province'],
                'city' => $input['city'],
                'area' => $input['area'],
                'address' => $input['address'],
                'addtime' => time(),
                'isdefault' => $input['isdefault'], //是否默认  1 是  2否
                'member_id' => $this->shopUser['member_id'],
            );
            $res = MemberAddress::addData($addData);
            if ($res) {
                return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '添加成功');
            } else {
                return ReturnMsg(CommonService::HTTP_CODE_FAIL, '添加失败');
            }
        } else { //编辑
            if (array_key_exists('mobile', $input)) {
                if (!Verify_Phone($input['mobile'])) {
                    return ReturnMsg(CommonService::HTTP_CODE_FAIL, '请输入有效手机号');
                }
            }
            if (array_key_exists('isdefault', $input)) {
                if ($input['isdefault'] == MemberAddress::MEMBER_ADDRESS_IS_DEFAULT) {
                    MemberAddress::where(array('member_id' => $this->shopUser['member_id']))->update(array('isdefault' => MemberAddress::MEMBER_ADDRESS_NOT_DEFAULT));
                }
            }
            MemberAddress::editDataById($input['id'], $input);
            return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '编辑成功');
        }


    }

    //根据id删除归还地址
    public function delReturnAddress(Request $request)
    {
        $input = $request->input();
        if (empty($input['id'])) {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, 'id为空');
        }
        MemberAddress::editDataById($input['id'], array('is_delete' => MemberAddress::MEMBER_ADDRESS_IS_DELETE));
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '删除成功');
    }


}
